--[[
Author: wangxiaoyan
Date: 2020-09-28 14:58:58
LastEditTime: 2020-10-30 23:24:04
FilePath: \Lua\View\PopUI\UISevenDayChallengeShop.lua

七日挑战（新手挑战）商店
--]]

require 'View/UIBase/UIBase'

UISevenDayChallengeShop = UIBase.New(PanelResNames.UISevenDayChallengeShop)
local _M = UISevenDayChallengeShop

_M.GOODS_MAX = 20 -- 可显示商品数量上限
_M.SHOP_ID = 4000 -- 临时使用每日商店

-- 初始化 
-- 注意这里使用的 . 而不是 : 
-- . 代表类型调用，: 代表对象调用
function _M.Init(func)
    UIBase.Init(_M,func)
end

-- 创建
function _M:OnCreate()
    self.shopCostDatas = nil
    self.shopDatas = nil
    self.ownPointLab = nil
    self.goodsItems = nil

    self:BindUI()
end

-- 销毁
function _M:OnDestroy()
    self.shopCostDatas = nil
    self.shopDatas = nil
    self.ownPointLab = nil
    self.goodsItems = nil
end

-- 显示
function _M:OnEnable()
    -- 每次打开商店都请求商店数据
    shopHandler.reqShopDataList(self.SHOP_ID)
end

-- 刷新
function _M:Refresh()
    local costData = self.shopCostDatas[1] -- 目前只取第一个
    self:SetIcon(self.ownPointSp,costData.currencyIcon)
    self.ownPointLab.text = logicMgr.ItemManager.getBagNumByTableID(tonumber(costData.currencyId))
    
    self.dynamicList.numItems = #self.shopDatas
end

function _M:CreateDynamicItem(itemObj,index)
    if self.goodsItems[itemObj] then 
        return
    end
    
    self.goodsItems[itemObj] = itemObj
    --local item = self.goodsItems[itemObj]
    --item.iconRoot = self:GetChild("iconRoot",itemObj)
    --item.quality = self:GetChild("quality",item.iconRoot)
    --item.icon = self:GetChild("icon",item.iconRoot)
    --item.num = self:GetChild("num",item.iconRoot)
    --item.priceSp = self:GetChild("priceSp",itemObj)
    --item.priceNum = self:GetChild("priceNum",itemObj)

end

function _M:RefreshDynamicItem(index , itemObj)
    self:CreateDynamicItem(itemObj, index)

    local item = self.goodsItems[itemObj]
    local goodsData = self.shopDatas[index]
    goodsData.shopId = self.SHOP_ID

    local ctrlItem = CtrlManager.GetCtrl(PanelNames.ShopItem);
    if ctrlItem ~= nil then
        ctrlItem.initData(item, goodsData);
    end
    --item.onClick:Clear();
    --self:RegisterOnClick(item,self.ClickGoods,self,index)
    --self:SetQuality(item.quality,goodsData.quality)
    --self:SetIcon(item.icon,goodsData.icon)
    --self:SetText(item.num,goodsData.remainCount)
    --self:SetIcon(item.priceSp,goodsData.moneyIcon)
    --self:SetText(item.priceNum,goodsData.price)
    -- item.maskGo:SetActive(tonumber(goodsData.remainCount) == 0)
end

-- 
function _M:BindUI()
    self.goodsItems = {}
    
    self.ownPointSp = self:GetChild("ownPointSp")
    self.ownPointLab = self:GetChild("ownPointLab")
    self.dynamicList = self:SetDynamicList(self:GetChild("dynamicList"),self.RefreshDynamicItem, self, true)

    self:RegisterOnClick(self:GetChild("back"),self.OnBack)
    self:RegisterEvent(Config.EventType.Fresh_Shop,self.MsgRefreshShop,self)
end

function _M:ClickGoods(index)
    local goodsData = self.shopDatas[index]
    goodsData.shopId = self.SHOP_ID
    if goodsData.itemType == EnumConst.ItemTypeEnum.Equip then
        local shopEquipAllInfo = logicMgr.ShopManager.equipBaseInfo(goodsData);
        showUI(PanelNames.UIEquipInfo,nil,shopEquipAllInfo,2);
    else
        showUI(PanelNames.UIBagItemInfo,goodsData,2);
    end
end

function _M:MsgRefreshShop()
    self.shopCostDatas = logicMgr.ShopManager.returnShopGold(self.SHOP_ID)
    self.shopDatas = nil
    local curShopData = logicMgr.ShopManager.returnShopData()
    if curShopData ~= nil then 
        self.shopDatas = curShopData.shopItems;
    end
    
    self:Refresh()
end

return _M